const faker = require("faker");
const fs = require("fs");

// 设置 Faker 为中文
faker.locale = "zh_CN";

// 生成模拟数据
function generateStudentData(numStudents, numClassrooms) {
  const students = [];
  for (let i = 1; i <= numStudents; i++) {
    const student = {
      name: faker.name.lastName() + faker.name.firstName(), // 随机生成中文姓名
      class: `班级${faker.datatype.number({ min: 1, max: 10 })}}`, // 随机班级
      examNumber: `2023${String(i).padStart(4, "0")}`, // 准考证号
      examRoom: faker.datatype.number({ min: 1, max: numClassrooms }), // 随机考场号
      seatNumber: faker.datatype.number({ min: 1, max: 30 }), // 随机座位号
    };
    students.push(student);
  }
  return students;
}

// 生成 100 名学生，5 个考场的数据
const studentData = generateStudentData(100, 5);

// 将数据保存为 CSV 文件
function saveToCSV(data, filename) {
  const header = ["姓名", "班级", "准考证号", "考场号", "座位号"].join(",");
  const rows = data.map((student) =>
    [
      student.name,
      student.class,
      student.examNumber,
      student.examRoom,
      student.seatNumber,
    ].join(","),
  );
  const csvContent = [header, ...rows].join("\n");

  fs.writeFileSync(filename, csvContent, "utf8");
  console.log(`数据已保存到 ${filename}`);
}

// 保存为 CSV 文件
saveToCSV(studentData, "student_exam_list.csv");
